package cn.le.thread;

/**
 * 如果某个线程在另一个小线程t 调用t.join(),此线程将被挂起,直到目标线程t结束才恢复(即t.isAlive()返回为假)
 *
 *
 */
public class Sleeper extends Thread {

    private int duration;
    public Sleeper(String name,int sleepTime)
    {
        super(name);
        this.duration = sleepTime;
        start();
    }

    public void run()
    {

        try
        {
            sleep(duration);//在指定的毫秒数内让当前正在执行的线程休眠（暂停执行），此操作受到系统计时器和调度程序精度和准确性的影响。
        }
        catch (InterruptedException e)
        {
            e.printStackTrace();
            System.out.println(getName()+"was interrupted "+isInterrupted()+" ");
        }
        System.out.println(getName()+" has awakened");
    }
}
class Joiner extends Thread{

    private Sleeper sleeper;

    public Joiner(String name,Sleeper sleeper)
    {
        super(name);
        this.sleeper = sleeper;
        start();
    }
    public void run()
    {
        try {
            sleeper.join(); //等待该线程终止
}
        catch (InterruptedException e)
                {
                e.printStackTrace();
                }
                System.out.println(getName()+" join completed");
                }
                }
class Joing {

    public static void main(String[] args) {

        Sleeper sleeper = new Sleeper("Sleepy",1500);
        Sleeper grumpy = new Sleeper("grumpy",1500);

        Joiner dopey = new Joiner("Dopey",sleeper);
        Joiner doc = new Joiner("Doc",grumpy);

        grumpy.interrupt();

    }

}










